package com.next

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
@Transactional(readOnly = true)
class TaskController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond Task.list(params), model: [taskCount: Task.count()]
    }

    def show(Task task)
    {
        respond task
    }

    def create()
    {
        def targetUri = request.getHeader("referer")
        respond new Task(params), model: [targetUri: targetUri]
    }

    @Transactional
    def save(Task task)
    {
        if (task == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (task.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond task.errors, model: [targetUri: params['targetUri']], view: 'create'
            return
        }

        task.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'task.label', default: 'Task'), task.id])
                // redirect task
                redirect uri: params['targetUri']
            }
            '*' { respond task, [status: CREATED] }
        }
    }

    def edit(Task task)
    {
        def username = springSecurityService?.getPrincipal()?.username
        def user = User.findByUsername(username)

        def targetUri = request.getHeader("referer")

        if (task.user == user)
        {
            respond task, model: [targetUri: targetUri]
        }
        else
        {
            flash.message = "没有编辑权限"
            redirect uri: targetUri
        }
    }

    @Transactional
    def update(Task task)
    {
        if (task == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (task.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond task.errors, model: [targetUri: params['targetUri']], view: 'edit'
            return
        }

        task.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'task.label', default: 'Task'), task.id])
                // redirect task
                redirect uri: params['targetUri']
            }
            '*' { respond task, [status: OK] }
        }
    }

    @Transactional
    def delete(Task task)
    {

        if (task == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        task.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'task.label', default: 'Task'), task.id])
                // redirect action:"index", method:"GET"
                redirect uri: request.getHeader("referer")
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
